package hipro.comments;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import hipro.Services;

public class Comments {
	// name space for instance soup Object
	private String namespace = "";
	// URL of web service
	private String url = "";

	private Services services = null;

	public Comments() {
		this.services = new Services();
	}

	public void setWebservice(String webservice) {
		this.namespace = webservice;
		this.url = webservice;
	}

	public boolean removeComment(String username, String password, int commentId) {

		SoapObject soapObject = new SoapObject(this.namespace, "removeComment");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("commentId", commentId);

		String json = this.services.call(this.url, "removeComment", soapObject);

		try {
			if (Integer.parseInt(json) > 0) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * Get comment by event id
	 * 
	 * @param username
	 * @param password
	 * @param eventId
	 * @return
	 */
	public ArrayList<hipro.entities.Comments> getCommentsByUsername(
			String username, String password) {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace,
				"getCommentsByUsername");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);

		String json = this.services.call(this.url, "getCommentsByUsername",
				soapObject);

		ArrayList<hipro.entities.Comments> comments = new ArrayList<hipro.entities.Comments>();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Comments>>() {
		}.getType();
		comments = gson.fromJson(json, collectionType);

		return comments;
	}

	/**
	 * Get comment by event id
	 * 
	 * @param username
	 * @param password
	 * @param eventId
	 * @return
	 */
	public ArrayList<hipro.entities.Comments> getCommentsByUserId(
			String username, String password, int userId) {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace,
				"getCommentsByUserId");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("userId", userId);

		String json = this.services.call(this.url, "getCommentsByUserId",
				soapObject);

		ArrayList<hipro.entities.Comments> comments = new ArrayList<hipro.entities.Comments>();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Comments>>() {
		}.getType();
		comments = gson.fromJson(json, collectionType);

		return comments;
	}

	/**
	 * Get comment by event id
	 * 
	 * @param username
	 * @param password
	 * @param eventId
	 * @return
	 */
	public ArrayList<hipro.entities.Comments> getCommentsByEventId(
			String username, String password, int eventId) {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace,
				"getCommentsByEventId");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("eventId", eventId);

		String json = this.services.call(this.url, "getCommentsByEventId",
				soapObject);

		ArrayList<hipro.entities.Comments> comments = new ArrayList<hipro.entities.Comments>();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Comments>>() {
		}.getType();
		comments = gson.fromJson(json, collectionType);

		return comments;
	}

	/**
	 * Add comment
	 * 
	 * @param username
	 * @param password
	 * @param comment
	 * @return
	 */
	public int addComment(String username, String password,
			hipro.entities.Comments comment) {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace, "addComment");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("data", gson.toJson(comment));

		String json = this.services.call(this.url, "addComment", soapObject);
		return Integer.parseInt(json);
	}

	/**
	 * Update comment
	 * 
	 * @param username
	 * @param password
	 * @param comment
	 * @return
	 */
	public boolean updateComment(String username, String password,
			hipro.entities.Comments comment) {
		try {
		Gson gson = new Gson();

		SoapObject soapObject = new SoapObject(this.namespace, "updateComment");
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("data", gson.toJson(comment));

		String json = this.services.call(this.url, "updateComment", soapObject);
		
			if (json.equals("true")) {
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Update comment "+e.toString());
			return false;
		}
	}

	/**
	 * Get comment
	 * 
	 * @param username
	 * @param password
	 * @param commentId
	 * @return
	 */
	public hipro.entities.Comments getComment(String username, String password,
			int commentId) {
		try {
			hipro.entities.Comments comment = new hipro.entities.Comments();

			SoapObject soapObject = new SoapObject(this.namespace, "getComment");
			soapObject.addProperty("username", username);
			soapObject.addProperty("password", password);
			soapObject.addProperty("commentId", commentId);

			String json = this.services
					.call(this.url, "getComment", soapObject);
			Gson gson = new Gson();
			comment = gson.fromJson(json, hipro.entities.Comments.class);

			return comment;
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Get comment " + e.toString());
			return null;
		}
	}
}
